package com.pajk.user.service.login.question;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.lang.time.DateUtils;
import com.pajk.user.model.SafetyQuestions;
import com.pajk.user.model.SafetyQuestions.Physique;
import com.pajk.user.model.enums.QuestionLevel;
import com.pajk.user.model.User;
import com.pajk.user.model.enums.Gender;
import com.pajk.user.model.enums.QuestionType;
import com.pajk.user.util.CommonUtils;
import com.pajk.user.util.DateUtil;

public class QuestionGenerator {
	private static String question1 = "生日是哪一天？";
	private static String question2 = "性别？";
	private static String question3 = "昵称是？";
	private static String question4 = "最近一次测评体质是什么？";
	private static String question5 = "身高是多少？";
	private static String question6 = "以下哪些是您将添加用户的家人？";

	
	public SafetyQuestions getsafetyQuestions(User user,Map<String,String> userMap) {
		SafetyQuestions safetyQuestions = new SafetyQuestions();
		Set<SafetyQuestions.SafetyQuestion> safetyQuestionSet = new HashSet<SafetyQuestions.SafetyQuestion>();
		ArrayList<SafetyQuestions.SafetyQuestion> safetyQuestionList = new ArrayList<SafetyQuestions.SafetyQuestion>();
		SafetyQuestions.SafetyQuestion safetyQuestion = null;
		
		if(user.getBirthday() != null) {//出生日期
			safetyQuestion = safetyQuestions.new SafetyQuestion(1,question1, QuestionLevel.THREE,QuestionType.RADIO);
			safetyQuestion.options = getOptionBirthdays(user.getId(),user.getBirthday(),safetyQuestion);
			safetyQuestionSet.add(safetyQuestion);
		}
		if(user.getGender() != null && !user.getGender().equals(Gender.INVALID_GENDER)) {//性别
			safetyQuestion = safetyQuestions.new SafetyQuestion(2,question2, QuestionLevel.ONE,QuestionType.RADIO);
			safetyQuestion.options = getOptionGenders(safetyQuestion);
			safetyQuestionSet.add(safetyQuestion);
		}
		if(user.getNick() != null && !"".equals(user.getNick().trim())) {//昵称
			safetyQuestion = safetyQuestions.new SafetyQuestion(3,question3,QuestionLevel.THREE,QuestionType.RADIO);
			safetyQuestion.options = getOptionNicks(user.getId(),user.getNick(),safetyQuestion);
			safetyQuestionSet.add(safetyQuestion);
		}
		if(user.getPhysique() != null && !"".equals(user.getPhysique().trim())) {//体质
			safetyQuestion = safetyQuestions.new SafetyQuestion(4,question4,QuestionLevel.THREE,QuestionType.RADIO);
			safetyQuestion.options = getOptionPhysiques(safetyQuestion);
			safetyQuestionSet.add(safetyQuestion);
		}
		if(user.getHeight() != null) {//身高
			safetyQuestion = safetyQuestions.new SafetyQuestion(5,question5, QuestionLevel.THREE,QuestionType.RADIO);
			safetyQuestion.options = getOptionHeights(user.getId(),user.getHeight(),safetyQuestion);
			safetyQuestionSet.add(safetyQuestion);
		}
		if(userMap != null && userMap.size() > 0) {//用户家人
			safetyQuestion = safetyQuestions.new SafetyQuestion(6,question6, QuestionLevel.THREE,QuestionType.CHECK);
			safetyQuestion.options = getOptionRelations(user.getId(),userMap,safetyQuestion);
			safetyQuestionSet.add(safetyQuestion);
		}
		Iterator<SafetyQuestions.SafetyQuestion> it = safetyQuestionSet.iterator();
		int i=0;
		while(it.hasNext()) {
			safetyQuestion = it.next();
			safetyQuestionList.add(safetyQuestion);
			i++;
			if(i==3) break;
		}
		safetyQuestions.questions = safetyQuestionList;
		return safetyQuestions;
	}
	
	private List<SafetyQuestions.SafetyQuestion.Option> getOptionBirthdays(Long userId,Date birthday,SafetyQuestions.SafetyQuestion safetyQuestion) {
		List<SafetyQuestions.SafetyQuestion.Option> options = new ArrayList<SafetyQuestions.SafetyQuestion.Option>(6);
		SafetyQuestions.SafetyQuestion.Option option = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		int num = (int)(userId % 6l);
		birthday = DateUtils.addMonths(birthday,-num);
		for(int i=0;i<6;i++) {
			option = safetyQuestion.new Option(DateUtil.format(birthday, "yyyy-MM-dd"),sdf.format(birthday));
			options.add(option);
			birthday = DateUtils.addMonths(birthday, 1);
		}
		return options;
	}
	
	private List<SafetyQuestions.SafetyQuestion.Option> getOptionGenders(SafetyQuestions.SafetyQuestion safetyQuestion) {
		List<SafetyQuestions.SafetyQuestion.Option> options = new ArrayList<SafetyQuestions.SafetyQuestion.Option>(2);
		SafetyQuestions.SafetyQuestion.Option option = safetyQuestion.new Option(Gender.MALE.name(),Gender.MALE.getNameCn());
		options.add(option);
		option = safetyQuestion.new Option(Gender.FEMALE.name(),Gender.FEMALE.getNameCn());
		options.add(option);
		return options;
	}
	
	private List<SafetyQuestions.SafetyQuestion.Option> getOptionHeights(Long userId,Integer height,SafetyQuestions.SafetyQuestion safetyQuestion) {
		List<SafetyQuestions.SafetyQuestion.Option> options = new ArrayList<SafetyQuestions.SafetyQuestion.Option>(6);
		SafetyQuestions.SafetyQuestion.Option option = null;
		int num = (int)(userId % 6l);
		height = height - 5*num;
		for(int i=0;i<6;i++) {
			option = safetyQuestion.new Option(height.toString(),height.toString()+"CM");
			options.add(option);
			height = height + 5;
		}
		return options;
	}
	
	private List<SafetyQuestions.SafetyQuestion.Option> getOptionPhysiques(SafetyQuestions.SafetyQuestion safetyQuestion) {
		List<SafetyQuestions.SafetyQuestion.Option> options = new ArrayList<SafetyQuestions.SafetyQuestion.Option>();
		SafetyQuestions.SafetyQuestion.Option option = null;
		for(Physique p : Physique.values()) {
			option = safetyQuestion.new Option(p.name(),p.getNameCn()); 
			options.add(option);
		}
		return options;
	}
	
	private List<SafetyQuestions.SafetyQuestion.Option> getOptionNicks(Long userId,String nick,SafetyQuestions.SafetyQuestion safetyQuestion) {
		List<SafetyQuestions.SafetyQuestion.Option> options = new ArrayList<SafetyQuestions.SafetyQuestion.Option>(6);
		SafetyQuestions.SafetyQuestion.Option option = null;
		int step = OptionDictionary.nicks.length/5;
		int startNum = (int)(userId % (long)(step));
		int insertNum = (int)(userId % 5l);
		for(int i=0;i<5;i++) {
			if(i == insertNum) {
				option = safetyQuestion.new Option("-1",nick);
				options.add(option);
			} 
			option = safetyQuestion.new Option(String.valueOf(startNum+step*i),nick.equals(OptionDictionary.nicks[startNum+step*i]) ? OptionDictionary.nicks[startNum+step*i]+"s":OptionDictionary.nicks[startNum+step*i]);
			options.add(option);
		}
		return options;
	}
	
	private List<SafetyQuestions.SafetyQuestion.Option> getOptionRelations(Long userId,Map<String,String> userMap,SafetyQuestions.SafetyQuestion safetyQuestion) {
		List<SafetyQuestions.SafetyQuestion.Option> options = new ArrayList<SafetyQuestions.SafetyQuestion.Option>(9);
		SafetyQuestions.SafetyQuestion.Option option = null;
		int step;
		step = OptionDictionary.names.length/8;
		int startNum = (int)(userId % (long)(step));
		int endIndex = 9 - userMap.size();
		Iterator<Entry<String, String>> it = userMap.entrySet().iterator();
		Entry<String, String> entry = it.next();
		Long id= Long.valueOf(entry.getKey());
		String userName = entry.getValue();
		//是否是中文
		if(CommonUtils.isChinese(userName)) {
			userName = userName.substring(0,1);
		} else {
			userName = "";
		}
		for(long i=0;i<endIndex;i++) {
			id++;
			userMap.put(id.toString(), userName + OptionDictionary.names[startNum+step*(int)i]);
		}
		for(Entry<String, String> e: userMap.entrySet()) {
			option = safetyQuestion.new Option(e.getKey(),e.getValue());
			options.add(option);
		}
		return options;
	}
	
}
